#$ARGV[0] is the 0/1 model log file.
#$ARGV[1] is number of rounds.
#$ARGV[2] is the numer of integrated rounds.(granulority)

$file = $ARGV[0];

$conditionalChannel = 1;
$totalNum = 100;
$totalRound = 1;

if($ARGV[1]!=0){
	$totalNum = $ARGV[1];
}
if($ARGV[2]!=0){
	$totalRound = $ARGV[2];
}

print "totalNum: $totalNum\n";
# if($ARGV[1]!=0){
	# $totalNum = $ARGV[1];
# }
# else {
	# $totalNum = 10000000;
# }

@pathname = split(/\\/,$file);
$len = @pathname;
$path;
for($i=$len-2;$i>=0;$i--){
	$path = "$pathname[$i]\\"."$path";
	print "$path\n";
}

$filename;
@tempfile = split(/\./,$pathname[$len-1]);
$filename = $tempfile[0];
print "file: $filename\n";

$channelNum = 16;
@samples;

@BBCMatric;
@IBCMatric;
@BICMatric;
@IICMatric;

#$totalIdle=0;
#$totalBusy=0;
@totalBusy;
@totalIdle;


open total, ">$path\\condTimeMatric\\CondTimeMatric_total_$totalRound.dat" or die "can not open file .\\CondTimeMatric_total_$totalRound.dat\n";

for($indxChannel=1;$indxChannel<=16;$indxChannel++){
print ("Round $indxChannel\n");
$conditionalChannel = $indxChannel;

for($i=0;$i<$channelNum+1;$i++){
		$BBCMatric[$i]=0;
		$IBCMatric[$i]=0;
		$BICMatric[$i]=0;
		$IICMatric[$i]=0;
		$totalBusy[$i]=0;
		$totalIdle[$i]=0;
}

$alreadyNum = 0;
$startTime = 0;
$previousTime = 0;
$rnd = 1;
open fin, "<$file" or die "can not open file $file!\n";
open fout, ">$path\\condTimeMatric\\CondTimeMatric_$conditionalChannel.dat" or die "can not open file .\\CondTimeMatric_$conditionalChannel.dat\n";
GETSAMPLE:{
	while (<fin>) {
		chomp;
		@info = split;

		$length = @info;
		if(($length-1)!=$channelNum){
			
			last GETSAMPLE;
		}
		
		if($startTime==0){
			$startTime=$info[0];
			$previousTime = $startTime;
		}
		
		if($info[0]-$previousTime>5000){
			$rnd++;
		}
		
		if($rnd>$totalRound){
			$sum = $totalBusy[$conditionalChannel]+$totalIdle[$conditionalChannel];
			
			print fout "$startTime ";
			print fout "$totalBusy[$conditionalChannel] $totalIdle[$conditionalChannel] ";
			#Column 1 2 3: Time ConditionalChannel totalRecord
			print total "$startTime $conditionalChannel $sum ";
			#Column 4-35: channel i busy/idle number 
			for($i=1;$i<$channelNum+1;$i++){
				#print fout "$totalBusy[$i] $totalIdle[$i] ";
				print total "$totalBusy[$i] $totalIdle[$i] ";
			}
			
			#Column 36-52: Pr(i=B|channel=B)-Pr(i=B)
			print fout "BB ";
			print total "BB ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalBusy[$conditionalChannel]!=0){
					$temp=$BBCMatric[$i]/$totalBusy[$conditionalChannel];
					$temp1=$BBCMatric[$i]/$totalBusy[$conditionalChannel] - $totalBusy[$i]/($totalBusy[$i]+$totalIdle[$i]);
					print fout "$temp ";
					print total "$temp1 ";
				}
				else {
					print fout "NA ";
					print total "NA ";
				}
			}
			#Column 53-69: Pr(i=I|channel=B)-Pr(i=I)
			print fout "IB ";
			print total "IB ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalBusy[$conditionalChannel]!=0){
					$temp=$IBCMatric[$i]/$totalBusy[$conditionalChannel];
					$temp1=$IBCMatric[$i]/$totalBusy[$conditionalChannel] - $totalIdle[$i]/($totalBusy[$i]+$totalIdle[$i]);
					print fout "$temp ";
					print total "$temp1 ";
				}
				else {
					print fout "NA ";
					print total "NA ";
				}
			}
			#Column 70-86: Pr(i=B|channel=I)-Pr(i=B)
			print fout "BI ";
			print total "BI ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalIdle[$conditionalChannel]!=0){
					$temp=$BICMatric[$i]/$totalIdle[$conditionalChannel];
					$temp1=$BICMatric[$i]/$totalIdle[$conditionalChannel]- $totalBusy[$i]/($totalBusy[$i]+$totalIdle[$i]);
					print fout "$temp ";
					print total "$temp1 ";
				}
				else {
					print fout "NA ";
					print total "NA ";
				}
			}
			#Column 87-103: Pr(i=I|channel=I)-Pr(i=I)
			print fout "II ";
			print total "II ";
			for($i=1;$i<$channelNum+1;$i++){
				if($totalIdle[$conditionalChannel]!=0){
					$temp=$IICMatric[$i]/$totalIdle[$conditionalChannel];
					$temp1=$IICMatric[$i]/$totalIdle[$conditionalChannel] - $totalIdle[$i]/($totalBusy[$i]+$totalIdle[$i]);
					print fout "$temp ";
					print total "$temp1 ";
				}
				else {
					print fout "NA ";
					print total "NA ";
				}
			}
			print fout "\n";
			print total "\n";
			$startTime = $info[0];
			for($i=0;$i<$channelNum+1;$i++){
				$BBCMatric[$i]=0;
				$IBCMatric[$i]=0;
				$BICMatric[$i]=0;
				$IICMatric[$i]=0;
				$totalBusy[$i]=0;
				$totalIdle[$i]=0;
			}
			$alreadyNum++;
			$rnd = 1;
		}
		
		for($i=1;$i<$channelNum+1;$i++){
				#if($i==$conditionalChannel && $info[$i]==1){
				if($info[$i]==1){
					$totalBusy[$i]++;
				}
				#if($i==$conditionalChannel && $info[$i]==0){
				if($info[$i]==0){
					$totalIdle[$i]++;
				}
				if($info[$i]==1 && $info[$conditionalChannel]==1){
					$BBCMatric[$i]++;
				}
				if($info[$i]==0 && $info[$conditionalChannel]==1){
					$IBCMatric[$i]++;
				}
				if($info[$i]==1 && $info[$conditionalChannel]==0){
					$BICMatric[$i]++;
				}
				if($info[$i]==0 && $info[$conditionalChannel]==0){
					$IICMatric[$i]++;
				}
		}
		$previousTime = $info[0];
		
		if($alreadyNum==$totalNum){
			last GETSAMPLE;
		}
	}
}
close fin;
close fout;

}
close total;